package com.naiterui.ehp.bs.ds.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.ds.common.utils.JwtUser;
import com.naiterui.ehp.bs.ds.common.utils.PageVO;
import com.naiterui.ehp.bs.ds.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.ds.modules.order.entity.ReturnOrder;
import com.naiterui.ehp.bs.ds.modules.order.feign.IReturnOrderFeginClient;
import com.naiterui.ehp.bs.ds.modules.order.mapper.OrderMapper;
import com.naiterui.ehp.bs.ds.modules.order.mapper.ReturnOrderItemMapper;
import com.naiterui.ehp.bs.ds.modules.order.mapper.ReturnOrderMapper;
import com.naiterui.ehp.bs.ds.modules.order.mapper.ReturnOrderOperateLogMapper;
import com.naiterui.ehp.bs.ds.modules.order.service.IReturnOrderService;
import com.naiterui.ehp.bs.ds.modules.order.vo.BaseOrderInfoVO;
import com.naiterui.ehp.bs.ds.modules.order.vo.ExchangeOrderDetailVO;
import com.naiterui.ehp.bs.ds.modules.order.vo.LogisticsCompanyVO;
import com.naiterui.ehp.bs.ds.modules.order.vo.OperateLogVO;
import com.naiterui.ehp.bs.ds.modules.order.vo.ReturnOrderDetailVO;
import com.naiterui.ehp.bs.ds.modules.order.vo.ReturnOrderItemVO;
import com.naiterui.ehp.bs.ds.modules.order.vo.ReturnOrderParamVO;
import com.naiterui.ehp.bs.ds.modules.order.vo.SupplementaryOrderDetailVO;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 *
 */
@Slf4j
@Service
@AllArgsConstructor
public class ReturnOrderServiceImpl extends ServiceImpl<ReturnOrderMapper, ReturnOrder> implements IReturnOrderService {

    private final OrderMapper orderMapper;
    private final ReturnOrderMapper returnGoodsOrderMapper;
    private final ReturnOrderItemMapper returnGoodsItemMapper;
    private final IReturnOrderFeginClient returnOrderFeginClient;
    private final ReturnOrderOperateLogMapper returnGoodsOperateLogMapper;

    @Override
    public PageVO<ReturnOrderDetailVO> queryPage(PageParamsVO pageParamsVO, ReturnOrderParamVO paramVO) {
        QueryWrapper<ReturnOrderDetailVO> entityWrapper = pageParamsVO.getEntityWrapper("t");
        //创建时间
        String startTime = paramVO.getStartTime();
        String endTime = paramVO.getEndTime();
        entityWrapper.between(startTime != null && endTime != null, "t.created_at", startTime, endTime);
        String orderSn = paramVO.getOrderSn();
        entityWrapper.eq(StringUtils.isNotBlank(orderSn), "t.order_sn", orderSn);
        String businessNo = paramVO.getBusinessNo();
        entityWrapper.eq(StringUtils.isNotBlank(businessNo), "t.business_no", businessNo);
        String phone = paramVO.getPhone();
        entityWrapper.eq(StringUtils.isNotBlank(phone), "ou.phone", phone);
        String userName = paramVO.getReceiver();
        entityWrapper.eq(StringUtils.isNotBlank(userName), "ou.receiver", userName);
        Integer type = paramVO.getType();
        if (type == null) {
            entityWrapper.in("t.type", 1, 2);
        } else {
            entityWrapper.eq("t.type", type);
        }

        Integer status = paramVO.getStatus();
        entityWrapper.eq(status != null, "t.status", status);
        JwtUser currentUser = SecurityUtils.getCurrentUser();
        Long warehouseId = currentUser.getWarehouseId();
        entityWrapper.eq("t.warehouse_id", warehouseId);

        Page<ReturnOrderDetailVO> page = this.baseMapper.getPage(pageParamsVO.getPage(), entityWrapper);
        return PageVO.toPageVo(page);
    }

    @Override
    public ReturnOrderDetailVO getReturnOrderDetail(Long id) {
        //查询退货单信息
        ReturnOrderDetailVO returnOrder = this.returnGoodsOrderMapper.getById(id);
        if (returnOrder != null) {
            //查询退货关联订单信息
            BaseOrderInfoVO orderInfoVO = this.orderMapper.getOrderInfoByOrderSn(returnOrder.getOrderSn());
            returnOrder.setOrderInfoVO(orderInfoVO);

            //查询退货列表
            List<ReturnOrderItemVO> products = this.returnGoodsItemMapper.findByBusinessNO(returnOrder.getBusinessNo());
            returnOrder.setReturnProducts(products);

            //查询该退货单操作日志列表
            List<OperateLogVO> operateLogs = this.returnGoodsOperateLogMapper.findByBusinessNO(returnOrder.getBusinessNo());
            returnOrder.setOperateLogs(operateLogs);
        }
        return returnOrder;
    }

    @Override
    public ExchangeOrderDetailVO getExchangeOrderDetail(Long id) {
        ReturnOrderDetailVO returnOrderVO = this.getReturnOrderDetail(id);
        if (returnOrderVO != null) {
            ExchangeOrderDetailVO exchangeOrderVO = new ExchangeOrderDetailVO();
            BeanUtils.copyProperties(returnOrderVO, exchangeOrderVO);
            //设置换货商品
            exchangeOrderVO.setExchanges(
                exchangeOrderVO
                    .getReturnProducts()
                    .stream()
                    .filter(s -> ReturnOrderItemVO.TYPE_EXCHANGE == s.getType())
                    .collect(Collectors.toList()));
            //设置退货商品
            exchangeOrderVO.setReturnProducts(
                exchangeOrderVO
                    .getReturnProducts()
                    .stream()
                    .filter(s -> ReturnOrderItemVO.TYPE_RETURN == s.getType())
                    .collect(Collectors.toList()));
            return exchangeOrderVO;
        }

        return null;
    }

    @Override
    public SupplementaryOrderDetailVO getSupplementaryOrderDetail(Long id) {
        //查询退货单信息
        ReturnOrderDetailVO returnOrder = this.getReturnOrderDetail(id);
        SupplementaryOrderDetailVO vo = new SupplementaryOrderDetailVO();
        BeanUtils.copyProperties(returnOrder, vo);
        //复制补寄商品信息
        vo.setExchanges(returnOrder.getReturnProducts());
        return vo;
    }

    @Override
    public void confirm(List<Long> ids) throws BusinessException {
        this.returnOrderFeginClient.confirm(ids);
    }

    @Override
    public List<LogisticsCompanyVO> logisticsCompanyList() {
        return this.baseMapper.getLogisticsCompanyList();
    }

}
